package controller

import (
	"gin/common/lib"
	"gin/middleware"
	"gin/model/body"
	"gin/service/recordService"
	"gin/util/jwt"
	"gin/util/map_builder"
	"gin/util/request"
	"github.com/gin-gonic/gin"
)

type RecordController struct {
}

func RecordRegister(router lib.RegisterRouterGroup, needLoginRouter lib.RegisterRouterGroup) {
	record := RecordController{}
	needLoginRouter.POST("/record/add", record.add)
	needLoginRouter.POST("/record/complete", record.complete)
	needLoginRouter.POST("/record/detail", record.detail)
	needLoginRouter.POST("/record/list", record.list)
	needLoginRouter.POST("/record/del", record.del)
}

// @Summary 添加运动记录
func (*RecordController) add(c *gin.Context) {
	// 绑定参数
	var p body.AddRecordParam
	request.Bind(c, &p)
	p.Uid = jwt.GetUid(c, true)

	recordService.Add(p)

	middleware.Success(c)
}

// @Summary 完成运动
func (*RecordController) complete(c *gin.Context) {
	var p body.CompleteRecordParam
	request.Bind(c, &p)

	uid := jwt.GetUid(c, true)
	recordService.Complete(uid, p)
	middleware.Success(c)

}

// @Summary 记录详情
func (*RecordController) detail(c *gin.Context) {
	var p body.IdParam
	request.Bind(c, &p)

	uid := jwt.GetUid(c, true)
	info := recordService.Detail(uid, p.Id)
	middleware.Success(c, map_builder.BuilderMap("info", info))
}

// @Summary 记录列表
func (*RecordController) list(c *gin.Context) {
	var p body.RecordListParam
	request.Bind(c, &p)

	p.Uid = jwt.GetUid(c, true)
	list, count := recordService.List(p)

	middleware.Success(c, map_builder.BuilderMap("list", list, "count", count))
}

// @Summary 刪除记录
func (*RecordController) del(c *gin.Context) {
	var p body.IdParam
	request.Bind(c, &p)

	uid := jwt.GetUid(c, true)
	recordService.Del(uid, p.Id)

	middleware.Success(c)
}
